import request from "./request.js"
import { Loading } from "element-ui"

let http = {
  /** get 请求
   * @param  {接口地址} url
   * @param  {请求参数} params
   */
  get: function (url, params) {
    let loadingInstance = Loading.service({
      lock: true,
      text: "请稍候",
      spinner: "el-icon-loading",
      background: "rgba(0, 0, 0, 0.7)",
      fullscreen: false,
      customClass: "loadingClass"
    })
    return new Promise((resolve, reject) => {
      request({
        url,
        method: "get",
        params
      })
        .then(response => {
          loadingInstance.close()
          resolve(response)
        })
        .catch(error => {
          loadingInstance.close()
          reject(error)
        })
    })
  },
  /** post 请求
   * @param  {接口地址} url
   * @param  {请求参数} params
   * @param  {请求的参数处理类型}
   * keyValue：默认的application/x-www-form-urlencoded，使用qs.stringify序列化。
   * json:使用application/json，不用处理参数
   */
  post: function (url, params, type = "keyValue") {
    let loadingInstance = Loading.service({
      lock: true,
      text: "请稍候",
      spinner: "el-icon-loading",
      background: "rgba(0, 0, 0, 0.7)",
      fullscreen: false,
      customClass: "loadingClass"
    })
    return new Promise((resolve, reject) => {
      request({
        url,
        method: "post",
        data: params,
        type
      })
        .then(response => {
          loadingInstance.close()
          resolve(response)
        })
        .catch(error => {
          loadingInstance.close()
          reject(error)
        })
    })
  },
  put: function (url, params) {
    let loadingInstance = Loading.service({
      lock: true,
      text: "请稍候",
      spinner: "el-icon-loading",
      background: "rgba(0, 0, 0, 0.7)",
      fullscreen: false,
      customClass: "loadingClass"
    })
    return new Promise((resolve, reject) => {
      request({
        url,
        method: "put",
        data: params
      })
        .then(response => {
          loadingInstance.close()
          resolve(response)
        })
        .catch(error => {
          loadingInstance.close()
          reject(error)
        })
    })
  },
  delete: function (url, params) {
    let loadingInstance = Loading.service({
      lock: true,
      text: "请稍候",
      spinner: "el-icon-loading",
      background: "rgba(0, 0, 0, 0.7)",
      fullscreen: false,
      customClass: "loadingClass"
    })
    return new Promise((resolve, reject) => {
      request({
        url,
        method: "delete",
        params
      })
        .then(response => {
          loadingInstance.close()
          resolve(response)
        })
        .catch(error => {
          loadingInstance.close()
          reject(error)
        })
    })
  },
  /** post 请求
   * @param  {接口地址} url
   * @param  {请求参数} params
   */
  postNoLoad: function (url, params) {
    return new Promise((resolve, reject) => {
      request({
        url,
        method: "post",
        data: params
      })
        .then(response => {
          resolve(response)
        })
        .catch(error => {
          reject(error)
        })
    })
  }
}
export default http
